<template>
  <div>
    <div class="card" style="margin-bottom: 10px;">
      <el-input v-model="data.categoryName" :prefix-icon="Search" style="width: 300px; margin-right: 10px"
                placeholder="请输入餐饮别名名称查询"></el-input>
      <el-button type="primary" style="margin-left: 10px;" @click="load">查询</el-button>
      <el-button type="info" @click="reset">重置</el-button>
    </div>
    <div class="card">
      <div style="margin-bottom: 10px;">
        <div style="margin-bottom: 10px">
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </div>
        <el-table :data="data.tableData">
          <el-table-column label="餐饮类别ID" prop="categoryId"></el-table-column>
          <el-table-column label="餐饮类别名字" prop="categoryName"></el-table-column>
          <el-table-column label="餐饮类目编号" prop="categoryType"></el-table-column>
          <el-table-column label="创建时间" prop="createTime"></el-table-column>
          <el-table-column label="修改时间" prop="updateTime"></el-table-column>
          <el-table-column label="操作" align="center" width="160">
            <template #default="scope">
              <el-button type="primary" @click="handleEdit(scope.row)">编辑</el-button>
              <el-button type="danger" @click="handleDel(scope.row.categoryId)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="card">
        <el-pagination background layout="prev, pager,next" @current-change="handelCurrentChange"
                       v-model:current-page="data.pageNum" v-model:page-size="data.pageSize" :total="data.total"/>
      </div>

      <!--模态框-->
      <el-dialog title="餐饮类别信息" width="40%" v-model="data.formVisible" :close-on-click-modal="false"
                 destroy-on-close>
        <el-form :model="data.form" label-width="100px" style="padding-right: 50px">
          <el-form-item label="餐饮类别名字" prop="categoryName">
            <el-input v-model="data.form.categoryName" autocomplete="off"/>
          </el-form-item>
          <el-form-item label="餐饮类目编号" prop="categoryName">
            <el-input v-model="data.form.categoryType" autocomplete="off"/>
          </el-form-item>
        </el-form>
        <template #footer>
      <span class="dialog-footer">
        <el-button @click="data.formVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">保 存</el-button>
      </span>
        </template>
      </el-dialog>

    </div>
  </div>

</template>

<script setup>
import {reactive} from "vue";
import {Search} from "@element-plus/icons-vue";
import request from "@/utils/request.js";
import {ElMessage} from "element-plus";
import router from "@/router/index.js";

const data = reactive({
  categoryName: '',
  tableData: [],
  pageNum: 1, //当前页码
  pageSize: 5, //每页个数
  total: 0,
  student: JSON.parse(localStorage.getItem('student-user') || '{}')
})

//请求数据
const load = () => {
  request.get("/seller/ProductCategory/selectPage", {
    params: {
      pageNum: data.pageNum,
      pageSize: data.pageSize,
      categoryName: data.categoryName
    }
  }).then(res => {
    console.log("分页数据:" + JSON.stringify(res));
    data.tableData = res.data?.list || [];
    data.total = res.data?.total || 0;
  })
}

//翻页
const handelCurrentChange = (pageNum) => {
  // 当翻页的时候重新加载数据即可
  load()
}

//重置
const reset = () => {
  data.categoryName = ''
  load()
}

//新增点击
const handleAdd = () => {
  data.form = {}
  data.formVisible = true //隐藏的对话框显示出来
}

//点击保存按钮
const save = () => {
  request.request({
    url: data.form.categoryId ? '/seller/ProductCategory/update' : '/seller/ProductCategory/save',
    method: data.form.categoryId ? 'PUT' : 'POST',
    data: data.form
  }).then(res => {
    if (res.code === '200') {
      load()    // 重新获取数据
      data.formVisible = false  // 关闭弹窗
      ElMessage.success("操作成功")
    } else {
      ElMessage.error(res.msg)
    }
  })
}

//点击编辑按钮，弹出模态框
const handleEdit = (row) => {
  data.form = JSON.parse(JSON.stringify(row))  // 拷贝行数据到表单对象，数据回显
  data.formVisible = true
}

//点击删除
const handleDel = (categoryId) => {
  ElMessageBox.confirm('删除数据后无法恢复，您确认删除吗？', '删除确认', {type: 'warning'}).then(res => {
    request.delete('/seller/ProductCategory/delete/' + categoryId).then(res => {
      if (res.code === '200') {
        load()    // 重新获取数据
        ElMessage.success("操作成功")
      } else {
        ElMessage.error(res.msg)
      }
    })
  }).catch(res => {
  })
}

/**
 * 调用方法获取后台数据
 */
load();

</script>